<?php
class authorize extends transactionBase {
    const version = "3.1";
    const delimitResponse = "TRUE";
    const delimiter = "|";
    const relayResponse = "FALSE";

	private $loginID;
    private $transactionKey;
	private $testRequest;
	private $authorizationCode;
    protected $postUrl;
    protected $postParams;
    protected $postResponse;
	protected $allowedTransactionTypes = array("AUTH_CAPTURE", "AUTH_ONLY", "CAPTURE_ONLY", "CREDIT", "PRIOR_AUTH_CAPTURE", "VOID");
	protected $allowedPaymentTypes = array("CC", "ECHECK");

	public function __construct() {
        $this->postUrl = "https://secure.authorize.net/gateway/transact.dll";
        $this->transactionType = "AUTH_CAPTURE";
        $this->paymentType = "CC";
	}

	public function authenticate($loginID, $transactionKey) {
		$this->loginID = $loginID;
		$this->transactionKey = $transactionKey;
	}

	public function getAuthorizationCode() {
		return $this->authorizationCode;
	}

	private function getPostString() {
        $str = "";
        $str .= "x_login=" . urlencode($this->loginID);
        $str .= "&x_tran_key=" . urlencode($this->transactionKey);
        $str .= "&x_version=" . urlencode(self::version);
        $str .= "&x_delim_data=" . urlencode(self::delimitResponse);
        $str .= "&x_delim_char=" . urlencode(self::delimiter);
        $str .= "&x_relay_response=" . urlencode(self::relayResponse);
        $str .= "&x_type=" . urlencode($this->transactionType);
        $str .= "&x_method=" . urlencode($this->paymentType);
        $str .= "&x_card_num=" . urlencode($this->cardnumber);
        if(!empty($this->cvv))
            $str .= "&x_card_code=" . urlencode($this->cvv);
        $str .= "&x_exp_date=" . urlencode($this->expiration);
        $str .= "&x_amount=" . urlencode($this->amount);
        if(!empty($this->description))
            $str .= "&x_description=" . urlencode($this->description);
        $str .= "&x_first_name=" . urlencode($this->fname);
        $str .= "&x_last_name=" . urlencode($this->lname);
        $str .= "&x_address=" . urlencode($this->address);
        $str .= "&x_state=" . urlencode($this->state);
        $str .= "&x_zip=" . urlencode($this->zip);
		if(!empty($this->testRequest))
			$str .= "&x_test_request=TRUE";
		if(!empty($this->transactionID) && in_array($this->transactionType, array("PRIOR_AUTH_CAPTURE", "CREDIT", "VOID")))
			$str .= "&x_trans_id=" . urlencode($this->transactionID);
		if(!empty($this->authorizationCode) && $this->transactionType == "CAPTURE_ONLY")
			$str .= "&x_auth_code=" . urlencode($this->authorizationCode);
			
        return $str;
    }

	private function parseResponse() {
        $respArr = explode(self::delimiter, $this->postResponse);
       $this->transactionID = $respArr[6];
        if($respArr[0] == "1")
            return true;
        else {
            $this->errorMessage = $respArr[3];
            return false;
        }
    }

	public function process() {
		$this->postParams = $this->getPostString();
		$request = curl_init($this->postUrl);
		curl_setopt($request, CURLOPT_HEADER, 0);
		curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($request, CURLOPT_POSTFIELDS, $this->postParams);
		curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
		$this->postResponse = curl_exec($request);
		curl_close($request);
		return $this->parseResponse();
	}

	public function setAuthorization() {
		$this->transactionType = "AUTH_ONLY";
	}

	public function setAuthorizationCode($authorizationCode) {
		$this->authorizationCode = $authorizationCode;
	}

	public function setSale() {
		$this->transactionType = "AUTH_CAPTURE";
	}

	public function setCapture() {
		$this->transactionType = "PRIOR_AUTH_CAPTURE";
	}

	public function setTestMode() {
		$this->postUrl = "https://test.authorize.net/gateway/transact.dll";
		//$this->testRequest = 1;
	}
}
?>
